package com.tw.community.components
{
	import flash.events.MouseEvent;
	
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.events.ResizeEvent;

	public class ResizableTitleWindow extends TitleWindow
	{
		//states
		private const NORMAL:String = "normal";
		private const MAXIMIZED:String = "maximized";
		private const MINIMIZED:String = "minimized";
		private const RESIZING:String = "resizing";
		//buttons
		private var _maxRestoreBtn:Button;
		private var _minBtn:Button;
		private var _resizeBtn:Button;
		//keeps track if window maximized
		private var _ws:String
		private function get _windowState():String{
			return _ws;
		}
		private function set _windowState(s:String):void{
			_ws = s;
		}
		//keeps track of the size that needs to get restored to
		private var _restoreWidth:Number;
		private var _restoreHeight:Number;
		private var _restoreX:Number;
		private var _restoreY:Number;
		
		public function ResizableTitleWindow()
		{
			super();
			this.showCloseButton = true;
			this.addEventListener(ResizeEvent.RESIZE, onResize);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseDown(evt:MouseEvent):void{
			
			if(mouseX > this.width - 20 && mouseX < this.width){
				if(mouseY > this.height - 20 && mouseY < this.height){
					trace("mouseX: " + mouseX)
					trace("mouseY: " + mouseY)
					startResize(evt);
				}
			}
		}
		private function onMouseUp(evt:MouseEvent):void{
			stopResize(evt);
		}
		override protected function createChildren():void{
			super.createChildren();
			createMaximizeButton();
			createMinimizeButton();
			//createResizeButton();
		}
		private function onMouseOut(evt:MouseEvent):void{
			if(_windowState == RESIZING){
				_windowState = NORMAL;
			}
		}
		private function onMouseMove(evt:MouseEvent):void{
			if(_windowState == RESIZING){
				this.width = mouseX + 20/2; 
				this.height = mouseY + 20/2;
			}
		}
		private function onResize(evt:ResizeEvent):void{
			positionMaxRestoreButton();
			positionMinButton();
			//positionResizeButton();
		}
		
		private function positionMaxRestoreButton():void{
			_maxRestoreBtn.x = this.width - 50; 
		}
		private function positionMinButton():void{
			_minBtn.x = _maxRestoreBtn.x - 10 - _minBtn.width; 
		}
		private function positionResizeButton():void{
			_resizeBtn.x = this.width - _resizeBtn.width;
			_resizeBtn.y = this.height - _resizeBtn.height;
		}
		
		private function createMaximizeButton():void{
			_maxRestoreBtn = new Button();
			_maxRestoreBtn.label = "+";
			_maxRestoreBtn.width = 20;
			_maxRestoreBtn.height = 20;
			_maxRestoreBtn.addEventListener(MouseEvent.CLICK, onMaxRestoreClicked);
			positionMaxRestoreButton()
			this.titleBar.addChild(_maxRestoreBtn);
		}
		
		private function createMinimizeButton():void{
			_minBtn = new Button();
			_minBtn.label = "-";
			_minBtn.width = 20;
			_minBtn.height = 20;
			_minBtn.addEventListener(MouseEvent.CLICK, onMinClicked);
			positionMinButton()
			this.titleBar.addChild(_minBtn);
		}
		
		private function createResizeButton():void{
			_resizeBtn = new Button();
			_resizeBtn.width = 20;
			_resizeBtn.height = 20;
			positionResizeButton();
			_resizeBtn.addEventListener(MouseEvent.MOUSE_DOWN, startResize);
			_resizeBtn.addEventListener(MouseEvent.MOUSE_UP, stopResize);
			this.titleBar.addChild(_resizeBtn);
		}
		
		private function startResize(evt:MouseEvent):void{
			_windowState = RESIZING;
		}
		private function stopResize(evt:MouseEvent):void{
			_windowState = NORMAL;
		}
		
		private function onMinClicked(evt:MouseEvent):void{
			minimize();
		}
		
		public function minimize():void{
			if(_windowState != MAXIMIZED){
				recordRestoreSizePos();
			}
			this.width = 150;
			this.y = parent.height - this.titleBar.height;
			this.x = parent.width - this.width;
			this.includeInLayout = false;
			_windowState = MINIMIZED;
		}
		
		private function onMaxRestoreClicked(evt:MouseEvent):void{
			switch(_windowState){
				case MAXIMIZED:
					restore();
					break;
				case MINIMIZED:
					restore();
					break;
				default:
					maximize();
					break;
			}
			/*
			if(_windowState != MAXIMIZED){
				if(_w
				maximize();	
			}else{
				restore();
			}*/
		}
		public function maximize():void{
			if(_windowState != MINIMIZED){
				recordRestoreSizePos();
			}
			this.width = this.parent.width;
			this.height = this.parent.height;
			this.x = 0;
			this.y = 0;
			_windowState = MAXIMIZED;
		}
		
		public function restore():void{
			this.width = _restoreWidth;
			this.height = _restoreHeight;
			this.x = _restoreX;
			this.y = _restoreY;
			_windowState = NORMAL;
		}
		
		private function recordRestoreSizePos():void{
			_restoreWidth = this.width;
			_restoreHeight = this.height;
			_restoreX = this.x;
			_restoreY = this.y;
		}
	}
}